package com.liangzhong.proxy.lottery
{
	import com.adobe.serialization.json.JSON;
	import com.common.utils.Debug;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.constants.ShareConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.ShareEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	
	import flash.net.URLVariables;
	
	import lz.controls.Alert;

	/**
	 * 抽奖 
	 * @author nick
	 * 
	 */	
	public class LotteryProxy extends BaseProxy
	{
		/**
		 *俱乐部proxy名字 
		 */
		public static const PROXYNAME:String = "LotteryProxyName";
		/**
		 *请求开始抽奖 
		 */
		public static const REQUEST_START_LOTTERY:String = "request_start_lottery";
		/**
		 *返回抽奖结果 
		 */
		public static const GET_START_LOTTERY:String = "get_start_lottery";
		/**
		 *请求排名数据 
		 */
		public static const REQUEST_RANKDATA:String = "request_rankData";
		
		/**
		 *返回排名数据 
		 */
		public static const GET_RANKDATA:String = "get_rankdata";
		
		public function LotteryProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 *请求开始 抽奖  
		 * @param $obj
		 * 
		 */
		public function requestStartLottery($obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.START_LOTTERY;;
			variables.type = $obj.type;
			variables.fillid = $obj.fillid;
			variables.filltype = $obj.filltype;
			variables.fillnum = $obj.fillnum;
			ProxyUtils.instance.webClient(getStartLottery,variables);
		}
		private function getStartLottery($data:Object):void
		{
//			Debug.echo("抽奖结果返回  "+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				if(obj.lottery == 2)
				{
//					trace("++++++");
				}
//				AppData.getInstance().bagDataManager.changeItemNum( obj.list.pid, obj.list.num);
//				AppData.getInstance().clubDatamanager.clubInfoVo.cashpoint = obj.list.cashpoint;
//				AppData.getInstance().clubDatamanager.clubInfoVo.tiantipoint = obj.list.honour;
				this.sendNotification(GET_START_LOTTERY,{fillid:obj.lottery,status:true});//status,true成功
				if(obj.list.pid == 255)// 橙色球员包 id 
				{
					var shareParam:Object = {};
					shareParam.type = ShareConst.Honour_Search_OrangePlayer;
					AppEventDispatcher.getInstance().dispatchEvent(new ShareEvent(ShareEvent.SHARE_SAY,shareParam));
				}
				//@lake 2013-04-19 task
				AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.HONOUR_DATA_CHANGE,{type:1});
			}else
			{
				this.sendNotification(GET_START_LOTTERY,{status:false});
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 * 请求排名数据 
		 * @param $obj
		 * 
		 */
		public function requestRankingData($obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.LOTTERYRANKINGS;;
			variables.type = $obj.type;
//			variables.fillid = $obj.fillid;
//			variables.filltype = $obj.filltype;
//			variables.fillnum = $obj.fillnum;
//			ProxyUtils.instance.webClient(getRankingData,variables);
			var funct:Function
			switch($obj.type)
			{
				case 1:
					
					funct = gettianTiData;
					break;
				case 2:
					funct = getMyData;
					break;
				case 3:
					funct = getFriendData;
					break;
				case 4:
					funct = getshootData;
					break;
			}
//			this.sendNotification(GET_RANKDATA,{type:$obj.type});
			ProxyUtils.instance.webClient(funct,variables);
		}
		/**
		 * 返回天梯数据 
		 * @param $data
		 * 
		 */
		private function gettianTiData($data:Object):void
		{
			Debug.echo("排名  "+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().lotteryDataManger.getdata(obj);
				this.sendNotification(GET_RANKDATA,{type:obj.type});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 *我的 
		 * @param $data
		 * 
		 */
		private function getMyData($data:Object):void
		{
			Debug.echo("排名  "+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().lotteryDataManger.getdata(obj);
				this.sendNotification(GET_RANKDATA,{type:obj.type});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 * 好友  
		 * @param $data
		 * 
		 */
		private function getFriendData($data:Object):void
		{
			Debug.echo("排名  "+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().lotteryDataManger.getdata(obj);
				this.sendNotification(GET_RANKDATA,{type:obj.type});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		/**
		 * 射手排名  
		 * @param $data
		 * 
		 */
		private function getshootData($data:Object):void
		{
			Debug.echo("排名  "+$data);
			var str:String = String($data);
			var results:Array = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().lotteryDataManger.getdata(obj);
				this.sendNotification(GET_RANKDATA,{type:obj.type});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}